package com.hfzy.ihk.web.aliyuncall.biz;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.hfzy.ihk.common.util.http.HttpClientUtils;
import com.hfzy.ihk.facade.calldata.service.EsOptRmiService;
import com.hfzy.ihk.web.aliyuncall.constant.RedisKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 将数据推送到Es 业务类
 */
@Component
public class PushDataToEsBiz {

    Logger logger = LoggerFactory.getLogger(PushDataToEsBiz.class);


    @Autowired
    RedisTemplate redisTemplate;
//    //retries 表示失败重试次数 默认两次
//    @Reference(version = "1.0.0",check = false,retries=0,timeout = 5000)
//    EsOptRmiService esOptRmiService;

    @Value("${pushdata.callhistory.data-url}")
    private String pushUlr;
    /**
     *
     * @param sessionId 对应的sessionId
     * @param jsonData
     * @param deleteKey  需要删除的deleteKey 推送成功就删除,不需要就传null
     */
    public void pushCallHistoryToEs(String sessionId,String jsonData,String deleteKey){
        try {
            logger.info("-------------------推送到ES");
             //String rs =esOptRmiService.createOrUpdateCloudCallHistoryBySessionId(jsonData);
             String rs =push(jsonData);
             logger.info("---->push es rs:{}",rs);
             if(rs.equals("1")) { //成功
                 logger.info("-------------------推送到ES成功sesionId:{}",sessionId);
             }else{ //失败
                 try {
                     redisTemplate.opsForSet().add(RedisKey.PUSH_TO_ES_FAILED_HISTORY, sessionId);
                 }catch (Exception e1){
                     logger.error("将推送ES 失败数据放入redis 失败！sessionId：{}",sessionId,e1);
                 }
             }
        }catch (Exception  e){
            try {
                logger.error("-->推送数据到es失败，放入redis",e);
                redisTemplate.opsForSet().add(RedisKey.PUSH_TO_ES_FAILED_HISTORY, sessionId);
            }catch (Exception e1){
                logger.error("将推送ES 失败数据放入redis 失败！sessionId：{}",sessionId,e1);
            }
        }

    }


    /**
     * 将录音文件推送到ES
     * @param sessionId
     * @param url
     * @param jsonData
     */
    public void pushRecordFileToEs(String sessionId,String url,String jsonData){
        try {
           // String rs = esOptRmiService.createOrUpdateCloudCallHistoryBySessionId(jsonData);
            String rs =push(jsonData);
            logger.info("----->push record rs:{}",rs);
            if(rs.equals("0")){
                try {
                    redisTemplate.opsForSet().add(RedisKey.PUSH_RECORD_URL_TO_ES_FAILED, sessionId+"::"+url);
                }catch (Exception e1){
                    logger.error("将推送ES 录音url 失败数据放入redis 失败！sessionId：{}",sessionId,e1);

                }
            }
        }catch (Exception  e){
            logger.error("-->推送数据到es失败，放入redis",e);
            try {
                redisTemplate.opsForSet().add(RedisKey.PUSH_RECORD_URL_TO_ES_FAILED, sessionId+"::"+url);
            }catch (Exception e1){
                logger.error("将推送ES 录音url 失败数据放入redis 失败！sessionId：{}",sessionId,e1);

            }
        }

    }


    public String push(String jsonStr) throws Exception{
        Long start = System.currentTimeMillis();
        Map<String,String> map = new HashMap<String,String>();
        map.put("jsonStr",jsonStr);
        String rs = HttpClientUtils.postMethod(pushUlr,map,null);
        logger.info("==========>http推送云呼记录：{}，返回：{},耗时：{}",jsonStr,rs,(System.currentTimeMillis()-start));
        if("true".equals(rs))
            return "1";
        else
            return "0";

    }
}
